/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.dao.impl;

import com.qdu.dao.CommentDao;
import com.qdu.entity.Comment;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ice
 */
public class CommentDaoImpl extends BaseDaoImpl1 implements CommentDao {

    @Override
    public int addComment(Comment c) {
        return executeUpdate("insert into Comment values(?,?,?,?,?)", c.getId(), c.getMBID(), c.getContents(), c.getTime(), c.getRating());
    }

    @Override
    public int updateComment(Comment c) {
        return executeUpdate("update Comment set Rating=? where Cid=?", c.getRating(), c.getCid());
    }

    @Override
    public int deleteComment(int cid) {
        return executeUpdate("delete Comment where Cid=?", cid);
    }

    @Override
    public List<Object[]> getCommentBookListById(int id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Object[]> list = new ArrayList<>();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select Book.BID,Book.BCover,Book.BName,Comment.Rating,Comment.contents,Comment.Cid from Comment,Book where id=? and Comment.MBID=Book.BID order by time desc");
            ps.setInt(1, id);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Object[]{rs.getString(1), rs.getString(2), rs.getString(3), rs.getFloat(4), rs.getString(5),rs.getInt(6)});
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    @Override
    public List<Object[]> getCommentMovieListById(int id) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Object[]> list = new ArrayList<>();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select Movie.MovieId,Movie.MCover,Movie.MChineseName,Comment.Rating,Comment.contents,Comment.Cid from Comment,Movie where id=? and Comment.MBID=Movie.MovieId order by time desc");
            ps.setInt(1, id);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Object[]{rs.getString(1), rs.getString(2), rs.getString(3), rs.getFloat(4), rs.getString(5),rs.getInt(6)});
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;
    }

    @Override
    public List<Object[]> getCommentListByMBId(String mbid) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Object[]> list = new ArrayList<>();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select [User].id,[User].Uname,[User].Uicon,Comment.Cid,Comment.id,Comment.contents,Comment.MBID,Comment.[time],Comment.Rating from Comment,[User] where MBID=? and Comment.id=[User].id order by time desc");
            ps.setString(1, mbid);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Object[]{rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4), rs.getInt(5), rs.getString(6), rs.getString(7), rs.getDate(8), rs.getFloat(9)});
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return list;

    }

}
